package com.hmall.api.cart.fallback;

import com.hmall.api.item.ItemClient;
import com.hmall.api.item.dto.ItemDTO;
import com.hmall.api.item.dto.OrderDetailDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 商品Feign接口的降级处理工厂类
 *
 * @Author mr.Du
 * @Date 2025/6/18 2025/6/18
 */
@Component
@Slf4j
public class ItemClientFallbackFactory implements FallbackFactory<ItemClient> {
    /**
     * 降级处理逻辑
     *
     * @param cause
     * @return
     */
    @Override
    public ItemClient create(Throwable cause) {
        return new ItemClient() {
            @Override
            public List<ItemDTO> queryItemByIds(Collection<Long> ids) {
                log.error("调用ItemClient#queryItemByIds方法出现异常,参数:{},异常:", ids, cause);
                //返回一个默认的空集合(表示在发生降级处理时给调用者一个固定的返回值)
                return Collections.emptyList();
            }

            @Override
            public void deductStock(List<OrderDetailDTO> items) {
                log.error("调用ItemClient#deductStock方法出现异常,参数:{},异常:",items,cause);
            }
        };
    }
}
